package com.youlai.boot.system.controller;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.entity.BackupRecord;
import com.youlai.boot.system.service.impl.BackupService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/backup")
public class BackupController {
    /**
     * 备份接口
     */
    @Autowired
    private BackupService backupService;
    @PostMapping("/auto")
    public Result<?> autoBackup(){
        try {
            backupService.performBackup(true);
            return Result.success("备份成功");
        }catch (Exception e){
            return Result.error("备份失败");
        }
    }
    @PostMapping("/manual")
    public Result<?> manualBackup(){
        try {
            backupService.performBackup(false);
            return Result.success("备份成功");
        }catch (Exception e){
            return Result.error("备份失败");
        }
    }


    @GetMapping("/list")
    public Result<List<BackupRecord>> listBackups(){
        return Result.success(backupService.listBackups());
    }
    @PostMapping("/pnpm/{id}")
    @Operation(description="恢复备份接口")
    public ResponseEntity<?> response(@PathVariable String id){
        try {
            backupService.restoreDatabase(id);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }


}
